###----Jython CTU4 Library
###----Author: Alex Ramsey
###

import sys
sys.path.append("C:\\jython2.5.1\\Lib\\")

import threading
import datetime

this = threading.local()


class funcs():
	this.p = ""
	def __init__(self, parent=None):
		this.p = parent
		
	def setMain(self, parent):
		this.p = parent
	
	def retrieveLine(self, string):
		return this.p.retrieveLine(string)
	
	def execute(self, string):
		this.p.execute(string)
	
	def transmit(self, command):
		this.p.transmit(command)
	
	def simpleTransmit(self, command):
		this.p.simpleTransmit(command)
	
	def waitfor(self, string, timeout):
		this.p.waitfor(string, timeout)
		return(this.p.waitforSucceeded)
	
	def termmsg(self, message):
		this.p.waitfor(">", 2000, 0, True, False)
		this.p.printMessage(message)
		this.p.transmit("")
	
	def captureWaitfor(self, command, start, end, timeout, lineout, verbosity):
		this.p.captureWaitfor(command, start, end, timeout, lineout, verbosity)
		return(this.p.waitforSucceeded)
	
	def capturePassed(self, string, verbosity):
		if(this.p.capturePassed(string, verbosity)):
			return True
		else:
			self.printErrorMessage(("Failed to find string '" + string + "'"), verbosity)
			return False
	
	def promptCli(self):
		this.p.transmit("cli")
		return this.p.waitfor("CTU4_>", 7000)
	
	def pause(self, time):
		this.p.waitfor("an unmatchable string", time)
	
	def printHeader(self, string):
		this.p.newLine()
		this.p.printMessage("********************************************************************************************")
		this.p.newLine()
		this.p.printMessage("** JYTHON SCRIPT: %s" % string)
		this.p.newLine()		
		this.p.printMessage("** Began on %s" % datetime.datetime.today().strftime("%A, %B %d, %Y %H:%M:%S"))
		this.p.newLine()		
		this.p.printMessage("********************************************************************************************")
		this.p.newLine()
		this.p.transmit("")
		
	def printFooter(self, string, errorSummary):
		this.p.waitfor("an unmatchable string", 100, 0, False)
		this.p.newLine()
		this.p.printMessage("********************************************************************************************")
		this.p.newLine()
		if(errorSummary):
			this.p.printMessage("** RESULT: %s" % string)
			this.p.printErrorSummary()
		else:
			this.p.printMessage("** RESULT: %s" % string)
		this.p.newLine()
		this.p.printMessage("** Ended on %s" % datetime.datetime.today().strftime("%A, %B %d, %Y %H:%M:%S"))
		this.p.newLine()
		this.p.printMessage("********************************************************************************************")
		this.p.newLine()
		this.p.transmit("")
		
	def printMessage(self, string, color, verbose):
		this.p.waitfor(">", 2000, 0, False)
		this.p.newLine()
		this.p.printMessage("********************************************************************************************", color)
		this.p.newLine()
		this.p.printMessage("** "+string+" (Errors="+str(this.p.getErrors())+")", color)
		this.p.newLine()
		this.p.printMessage("********************************************************************************************", color)
		this.p.newLine()
		this.p.transmit("")
	
	def printErrorMessage(self, string, verbose):
		this.p.newLine()
		this.p.printMessage("**   Error: " + string + " (Errors=" + str(this.p.getErrors()) + ")")
		this.p.newLine()
	
	def addError(self, string):
		this.p.addError(string)
